<?php

/**
 * 模型对应数据库表 "subsite_category".
 *
 * 以下是可用表subsite_category的栏位:
 * @property integer $id
 * @property string $pid
 * @property string $title
 * @property string $info
 * @property string $logo
 * @property string $thum
 * @property string $page_title
 * @property string $page_keywords
 * @property string $page_description
 * @property string $product_model
 * @property string $sort
 * @property integer $status
 * @property string $game_id
 */
class SubsiteCategory extends CActiveRecord {

    public $type_array = array(
        '1' => '产品',
        '2' => '资讯',
    );

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return SubsiteCategory the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_site_category';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('title, title_alias', 'required'),
            array('menu, type, status, hot, infosort_id', 'numerical', 'integerOnly' => true),
            array('pid, sort, game_id', 'length', 'max' => 11),
            array('title, logo, thum, page_title, page_keywords', 'length', 'max' => 255),
            array('product_model', 'length', 'max' => 20),
            array('info, page_description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, pid, title,title_alias, info, logo, thum, page_title, page_keywords, page_description, product_model, sort, status, game_id, hot', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'pid' => '上级',
            'title' => '名称',
            'info' => '内容',
            'logo' => 'Logo',
            'thum' => '缩略图',
            'page_title' => 'SEO标题',
            'page_keywords' => 'SEO关键字',
            'page_description' => 'SEO描述',
            'product_model' => '栏目类型',
            'sort' => '序号',
            'status' => '状态',
            'game_id' => '游戏',
            'menu' => '菜单显示',
            'type' => '类型',
            'title_alias' => '别名',
            'hot' => '热门产品',
            'infosort_id' => '资讯分类',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('pid', $this->pid);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('menu', $this->menu, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('product_model', $this->product_model, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('game_id', $this->game_id, true);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取分类名称
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getSortNameById($id) {
        $data = $this->findByPk($id);
        return empty($data) ? '一级目录' : $data->title;
    }

    /**
      +-----------------------------------------------------
     * 说明 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getCategoryTitle($cObj) {
        $id = $cObj->id;
        $rStirng = "<span class='title_catagory fl'>┗</span><span class='fl' style='margin-right:20px;'><input size='30' type='text' value='" . $cObj->title . "' field='title' rel='" . $cObj->id . "' onChange='saveChange(this)'></input></span>";
        //存在下级，需要展示下级的按钮
        $scc = $this->IsHaveLowerLevel($id);
        if ($scc) {
            $rStirng .= '<a title="' . $scc . ' 个子栏目"  rel="' . $cObj->id . '" href="javaScript:void(0)" onclick="dropLowerLevel(this)" getdata="0" class="gradient-btn" >展开</a>';
        }
        $rStirng .= '<a title="查看" rel="' . $cObj->id . '" parent="1" href="' . Yii::app()->createUrl('manage/subsiteCategory/view', array('id' => $cObj->id)) . '"  class="gradient-btn">查看</a>';
        $rStirng .= '<a title="添加子栏目" rel="' . $cObj->id . '" parent="1" href="' . Yii::app()->createUrl('manage/subsiteCategory/create', array('pid' => $cObj->id)) . '"  class="gradient-btn">添加子栏目</a>';
        $rStirng .= '<a title="编辑" rel="' . $cObj->id . '" parent="1" href="' . Yii::app()->createUrl('manage/subsiteCategory/update', array('id' => $cObj->id)) . '"  class="gradient-btn">编辑</a>';
        $rStirng .= '<a title="删除" parent="1"  rel="' . $cObj->id . '"  href="javaScript:void(0)" onclick="deleteCategory(this)" class="gradient-btn">删除</a>';
        return $rStirng;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取目录是否有下级 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function IsHaveLowerLevel($cid) {
        $data = $this->findAll("pid='" . $cid . "'");
        return empty($data) ? false : count($data);
    }

    /**
     * 获取网站栏目树
     * @return type
     */
    public function getCategoryTree() {
        $cr = new CDbCriteria;
        $cr->compare('status', 1);
        $cr->order = 'pid ASC, sort DESC';
        $data = $this->findAll($cr);
        $tree = array();
        foreach ($data as $v) {
            if ($v->pid == 0) {
                $tree[$v->id] = array('master' => $v);
            } else {
                $tree[$v->pid]['sub'][] = $v;
            }
        }
        return $tree;
    }

    /**
     * 获取热门产品栏目
     * @return array
     */
    public function getHotCategory() {
        $category = array();
        $cr = new CDbCriteria;
        $cr->compare('status', 1);
        $cr->compare('hot', 1);
        $cr->order = 'sort DESC';
        foreach ($this->findAll($cr) as $v) {
            $category[$v->id]['category'] = $v;
            if ($v->pid > 0) {
                $category[$v->id]['master'] = $this->findByPk($v->pid);
            }
        }
        return $category;
    }

    /**
     * 获取子栏目
     * @param type $pid
     * @return type
     */
    public function getSubCategorys($pid) {
        $cr = new CDbCriteria;
        $cr->compare('pid', $pid);
        $cr->compare('status', 1);
        $cr->order = 'sort DESC';
        return $this->findAll($cr);
    }

    /**
     * 内容页
     * @param type $title_alias
     * @return type
     */
    public function getPage($title_alias) {
        $cr = new CDbCriteria;
        $cr->compare('title_alias', $title_alias);
        return $this->model()->find($cr);
    }
    
    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        $size = strlen($val)>50 ? 50 : strlen($val);
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)", 'size' => $size));
    }

}